﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StoragePeriod.Data;
using StoragePeriod.Common;
using StoragePeriod.Web.Common;
using StoragePeriod.BLL.RawDict;
using StoragePeriod.Model.RawDict;

namespace StoragePeriod.Web.SysManage
{
    public partial class RawMaterialManage : SecurityPage
    {
        T_RawMaterial tRawMaterial = new T_RawMaterial();//原料操作
        private List<E_RawMaterial> rmlistAll; //所有原料数据集合
        T_RawMaterials tRawMaterialType = new T_RawMaterials(); //原料大类
        T_RawMaterialsInfo tRawMaterialTypeInfo = new T_RawMaterialsInfo();//原料细类

        public override string PrivCode()
        {
            return "c5c7dd28e61be01a";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            E_RawMaterial eRawMaterial = new E_RawMaterial();
            eRawMaterial.StrWhere = "";//获取所有原料数据
            rmlistAll = tRawMaterial.GetRawListByWhere(eRawMaterial);
            DropDataBing();
            Drop_RawType_SelectedIndexChanged(null, null);
            RawBingData();
            OldRawBingData();
        }

        //加载子节点
        private TreeNode GetRawByTypeId(TreeNode tn)
        {
            int Pid = int.Parse(tn.Value);
            int typeid = Convert.ToInt32(Request.QueryString["RawTypeId"].ToString());
            List<RawMaterialType> RawTypeList = RawMaterialType.Where("ParentId=@0", Pid).ToList();
            RawTypeList.ForEach(p =>
            {
                TreeNode childNode = new TreeNode()
                {
                    Text = p.TypeName,
                    Value = p.id.ToString(),
                    Target = "0",
                    ShowCheckBox = true
                };
                childNode = GetRawByTypeId(childNode);
                tn.ChildNodes.Add(childNode);
            });

            int TypeId = int.Parse(tn.Value);

            List<E_RawMaterial> rmlist = new List<E_RawMaterial>();
            switch (typeid)
            {
                case 1:
                    rmlist = rmlistAll.Where(o => o.PeriodTypeID == TypeId).ToList();
                    break;
                case 2:
                    rmlist = rmlistAll.Where(o => o.PeriodTypeStorageID == TypeId).ToList();
                    break;
                case 3:
                    rmlist = rmlistAll.Where(o => o.PeriodTypeKnowledgeID == TypeId).ToList();
                    break;
                case 4:
                    rmlist = rmlistAll.Where(o => o.PeriodTypeStorKnowledgeID == TypeId).ToList();
                    break;
            }
            rmlist.ForEach(a =>
            {
                tn.ChildNodes.Add(new TreeNode()
                {
                    Text = a.RAWName,
                    Value = a.id.EToString(),
                    Target = "1",
                    ShowCheckBox = true
                });
                rmlistAll.Remove(a);
            });
            return tn;
        }

        //绑定原料树形节点
        private void RawBingData()
        {
            tvNewRawView.Nodes.Clear();
            int typeid = Convert.ToInt32(Request.QueryString["RawTypeId"].ToString());
            List<RawMaterialType> RawTypeList = RawMaterialType.Where("ParentId=@0 and Typeinfoid=@1", 0, Convert.ToInt32(EnumData.TypeInfo.食品检测)).ToList();
            RawTypeList.ForEach(p =>
            {
                TreeNode childNode = new TreeNode()
                {
                    Text = p.TypeName,
                    Value = p.id.EToString(),
                    Target = "0",
                    ShowCheckBox = true

                };
                childNode = GetRawByTypeId(childNode);
                tvNewRawView.Nodes.Add(childNode);
            });
            this.tvNewRawView.CollapseAll();
        }

        //绑定原始原料树形菜单
        private void OldRawBingData()
        {
            tvRawView.Nodes.Clear();

            //获取符合要求的原料大类列表
            List<E_RawMaterials> RawType = new List<E_RawMaterials>();
            E_RawMaterials eRawMaterials = new E_RawMaterials();
            eRawMaterials.StrWhere = string.Format("TypeName='{0}' or TypeName='{1}' or TypeName='{2}' or TypeName='{3}'", "肉类", "水产类", "蔬菜类", "水果类");
            RawType = tRawMaterialType.GetModelListByWhere(eRawMaterials);

            //获取原料细类列表
            List<E_RawMaterialsInfo> RawTypeInfo = new List<E_RawMaterialsInfo>();
            E_RawMaterialsInfo eRawMaterialsInfo = new E_RawMaterialsInfo();
            eRawMaterialsInfo.StrWhere = "";//获取所有的分类信息
            RawTypeInfo = tRawMaterialTypeInfo.GetModelListByWhere(eRawMaterialsInfo);

            RawType.ForEach(p =>
            {
                TreeNode childNode = new TreeNode()
                {
                    Text = p.TypeName,
                    Value = p.id.ToString(),
                    Target = "0",
                    ShowCheckBox = true
                };
                //添加细类
                List<E_RawMaterialsInfo> TypeInfo = RawTypeInfo.Where(o => o.RMID == p.id).OrderBy(t => t.Name).ToList();
                TypeInfo.ForEach(k =>
                {
                    TreeNode TypeInfoNode = new TreeNode()
                    {
                        Text = k.Name,
                        Value = k.id.ToString(),
                        Target = "0",
                        ShowCheckBox = true
                    };
                    //添加原料
                    List<E_RawMaterial> RawList = this.rmlistAll.Where(w => w.RAWIDS == k.id).OrderBy(t => t.RAWName).ToList();
                    RawList.ForEach(r =>
                    {
                        TreeNode RawNode = new TreeNode()
                        {
                            Text = r.RAWName,
                            Value = r.id.ToString(),
                            Target = "1",
                            ShowCheckBox = true
                        };
                        TypeInfoNode.ChildNodes.Add(RawNode);
                    });
                    childNode.ChildNodes.Add(TypeInfoNode);
                });
                this.tvRawView.Nodes.Add(childNode);
            });
            this.tvRawView.CollapseAll();
        }

        //加载类别下拉菜单
        private void DropDataBing()
        {
            int typeid = Convert.ToInt32(Request.QueryString["RawTypeId"].ToString());
            IQueryable<RawMaterialType> rawType = RawMaterialType.Where("ParentId=@0 and Typeinfoid=@1", 0, Convert.ToInt32(EnumData.TypeInfo.食品检测));
            this.Drop_RawType.DataSource = rawType;
            this.Drop_RawType.DataValueField = "id";
            this.Drop_RawType.DataTextField = "TypeName";
            this.Drop_RawType.DataBind();
            this.Drop_RawType.SelectedIndex = 0;
        }

        //加载子类
        protected void Drop_RawType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.Drop_RawType.SelectedItem != null)
            {
                int Pid = Convert.ToInt32(this.Drop_RawType.SelectedValue);
                int typeid = Convert.ToInt32(Request.QueryString["RawTypeId"].ToString());
                IQueryable<RawMaterialType> rawType = RawMaterialType.Where("ParentId=@0", Pid);
                this.Drop_RawTypeInfo.DataSource = rawType;
                this.Drop_RawTypeInfo.DataValueField = "id";
                this.Drop_RawTypeInfo.DataTextField = "TypeName";
                this.Drop_RawTypeInfo.DataBind();
            }
        }

        protected void Btn_SetType_Click(object sender, EventArgs e)
        {
            if (this.HidSelectTypeid.Value == "")
                return;

            int typeid = Convert.ToInt32(Request.QueryString["RawTypeId"].ToString());
            int SetTypeId = int.Parse(this.HidSelectTypeid.Value);
            List<TreeNode> checkNodes = this.tvRawView.CheckedNodes.AllNodes().Where(p => p.Target == "1").ToList();
            checkNodes.ForEach(p =>
            {

                E_RawMaterial md = tRawMaterial.GetModel(int.Parse(p.Value));
                switch (typeid)
                {
                    case 1:
                        md.PeriodTypeID = SetTypeId;
                        break;
                    case 2:
                        md.PeriodTypeStorageID = SetTypeId;
                        break;
                    case 3:
                        md.PeriodTypeKnowledgeID = SetTypeId;
                        break;
                    case 4:
                        md.PeriodTypeStorKnowledgeID = SetTypeId;
                        break;
                }
                tRawMaterial.Update(md);
            });

            E_RawMaterial eRawMaterial = new E_RawMaterial();
            eRawMaterial.StrWhere = "";//获取所有原料数据
            rmlistAll = tRawMaterial.GetRawListByWhere(eRawMaterial);
            RawBingData();
            OldRawBingData();
            UtilityUI.Load(this.upPanelMain, "$('#MainContent_Drop_RawType').trigger('change');alert('设置成功！');");
        }

        protected void Btn_ClearType_Click(object sender, EventArgs e)
        {
            int typeid = Convert.ToInt32(Request.QueryString["RawTypeId"].ToString());
            List<TreeNode> checkNodes = this.tvNewRawView.CheckedNodes.AllNodes().Where(p => p.Target == "1").ToList();
            checkNodes.ForEach(p =>
            {
                E_RawMaterial md = tRawMaterial.GetModel(int.Parse(p.Value));
                switch (typeid)
                {
                    case 1:
                        md.PeriodTypeID = null;
                        break;
                    case 2:
                        md.PeriodTypeStorageID = null;
                        break;
                    case 3:
                        md.PeriodTypeKnowledgeID = null;
                        break;
                    case 4:
                        md.PeriodTypeStorKnowledgeID = null;
                        break;
                }
                tRawMaterial.Update(md);
            });

            E_RawMaterial eRawMaterial = new E_RawMaterial();
            eRawMaterial.StrWhere = "";//获取所有原料数据
            rmlistAll = tRawMaterial.GetRawListByWhere(eRawMaterial);
            RawBingData();
            OldRawBingData();
            UtilityUI.Alert(this.upPanelMain, "取消成功！");
        }
    }
}